<?php
    class Model_Student extends CI_Model{
        private $logged_in=false;
        public $sutdent_id;
        public $sutdent_name;
        public $password;
        
        
        function __construct() {
        //$this->check_message();
            $this->check_login();
            if($this->logged_in){
            ////
            }else {
             ///
            }
        }

        public function is_logged_in(){
            return $this->logged_in;
        }

        public function login($student_id) {
            if($student_id){
                $this->student_id=$student_id;
                $this->session->set_userdata('student_id',$student_id);
                $this->logged_in = true;
            }
        }
        
        public function authenticate($student_name="",$password=""){
            $this->load->database();
            $student_name = $this->db->escape($student_name);
            $query = $this->db->query($this->authenticate_sql($student_name,$password));
            $query=$query->result_array($query);
            if(empty($query)){
                echo 'no';
                return false;
            }else{
            $query_array=$query[0];
            return !empty($query_array) ? array_shift($query_array) : false;
            }
        }

        private function authenticate_sql($student_name,$password){
            $sql = "SELECT * ";
            $sql .="FROM wse.student"; 
            $sql .=" WHERE student_name={$student_name} "; 
            $sql .="AND password = {$password} ";
            $sql .="LIMIT 1";
            return $sql;
        }
        
        public function logout() {
          $this->session->unset_userdata(student_id);
          unset($this->student_id);
          $this->logged_in= false;
          redirect("student/login");
        }

        public function student_info($student_id){

            $this->db->select('student_id,student_name');
            $this->db->where('student_id', $student_id);
            $query=$this->db->get('student');
           if($query->num_rows($query)>0){
               $row=$query->row();
               return $row;
           } 

        }

        public function get_nick_name_id($student_id){
            $this->db->select('nick_name');
            $this->db->where('student_id',$student_id);
            $query=$this->db->get('student');
            if($query->num_rows($query)>0){
                $row=$query->row();
                $row->nick_name;
                return $row->nick_name;
            }
        }
        
        private function check_login(){
            if($this->session->userdata("student_id")){
                $this->student_id=$this->session->userdata("student_id");
                $this->logged_in=true;
                }else{
                    unset($this->student_id);
                $this->logged_in = false;
            }
        }
        
    }
?>
